<template>
  <div>
    <!-- 搜索布局 -->
    <el-form :inline="true" id="w1" :model="search" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="xuan">删除选中</el-button>
      </el-form-item>
      <el-form-item id="w2" label="用户名">
        <el-input v-model="search.name" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchs">查询</el-button>
        <el-button type="primary" @click="chong">重置</el-button>
        <el-button type="primary" @click="addEdit = true">添加</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格数据渲染 -->
    <el-table
      id="tab"
      :data="reslist"
      style="width: 100%"
      @selection-change="sel"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="nickname" label="用户名" width="180">
      </el-table-column>
      <el-table-column prop="mobile" label="账号余额" width="180">
      </el-table-column>
      <el-table-column label="头像" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px; border-radius: 50%"
            :src="scope.row.avatar"
            :preview-src-list="imglist"
            @click="imglist.push(scope.row.avatar)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="adress" label="地址" width="220">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="del(scope.$index)" type="text" size="small"
            >删除</el-button
          >
          <el-button type="text" size="small" @click="bian(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      id="fy"
      :current-page="pageNum"
      :page-size="pageSize"
      background
      layout="prev, pager, next"
      :total="list.length"
      @current-change="change"
    >
    </el-pagination>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑信息" :visible.sync="showEdit">
      <el-form :model="edit">
        <el-form-item label="用户名">
          <el-input v-model="edit.nickname"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="edit.adress"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showEdit = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
      <!-- 添加弹出框 -->
    </el-dialog>
    <el-dialog title="添加信息" :visible.sync="addEdit">
      <el-form :model="add">
        <el-form-item label="ID">
          <el-input v-model="add.id"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="add.nickname"></el-input>
        </el-form-item>
        <el-form-item label="金额">
          <el-input v-model="add.mobile"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="add.adress"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addEdit = false">取 消</el-button>
        <el-button type="primary" @click="addue">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 所有的数据
      list: [],
      // 搜索备份的数据方便搜索
      list1: [],
      // 点击查看大图
      imglist: [],
      // 搜索的值
      search: {
        name: "",
      },
      // 分页
      pageNum: 1,
      pageSize: 5,
      // 编辑
      edit: {
        nickname: "",
        adress: "",
      },
      // 控制编辑弹框出现的变量
      showEdit: false,
      // 控制添加弹框出现的变量
      addEdit: false,
      //  添加
      add: {
        nickname: "",
        adress: "",
        id: "",
        mobile: "",
      },
    };
  },
  // 获取数据
  mounted() {
    if (localStorage.getItem("stulist")) {
      this.list = JSON.parse(localStorage.getItem("stulist"));
      this.list1 = JSON.parse(localStorage.getItem("stulist"));
    } else {
      this.$axios("/list.json").then((res) => {
        this.list = res.data;
        this.list1 = res.data;
      });
    }
  },
  // 分页
  computed: {
    reslist() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
  methods: {
    // 把删除修改的数据存在本地存储中
    savelocal() {
      localStorage.setItem("stulist", JSON.stringify(this.list));
    },
    // 把所有要选中删除的id存起来存成一个字符串
    sel(val) {
      let res = val.map((item) => {
        return item.uid;
      });
      this.removeIds = res.join(",");
    },
    // 选中删除
    xuan() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list = this.list.filter((item) => {
            if (this.removeIds.indexOf(item.uid) == -1) {
              return item;
            }
          });
          this.savelocal();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 搜索
    searchs() {
      this.list = this.list1.filter((item) => {
        return item.nickname.includes(this.search.name);
      });
    },
    change(val) {
      this.pageNum = val;
    },
    // 删除
    del(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list.splice(index, 1);
          this.savelocal();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  编辑
    bian(item) {
      this.showEdit = true;
      this.edit = item;
    },
    // 保存编辑
    update() {
      this.showEdit = false;
      this.list.forEach((item) => {
        if (item.uid == this.edit.uid) {
          item.nickname == this.edit.nickname;
          item.adress == this.edit.adress;
        }
      });
      this.savelocal();
    },
    // 重置
    chong() {
      this.list = this.list1;
      this.search.name = "";
    },
    // 添加
    addue() {
      this.addEdit = false;
      this.list.push(this.add);
      this.savelocal();
    },
  },
};
</script>
<style  scoped>
#tab {
  margin-top: -10px;
}
#w1 {
  margin-top: 10px;
  margin-left: -650px;
}
#w2 {
  line-height: 10px;
}
#fy {
  float: left;
  margin-top: 15px;
}
</style>